﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FamilyJobsUI.DB1;
using FamilyJobsUI.Extensions;

namespace FamilyJobsUI.Controllers
{
    [RequireHttps]
    public class UsersController : Controller
    {
        private DB1.FamilyJobsDB db = new DB1.FamilyJobsDB();

        // GET: Users
        public ActionResult Index()
        {
            var rawUsers = db.T_Users;

            var userEditModelList = rawUsers
                .Project().To<Models.UsersEditModel>();

            return View(userEditModelList );
        }

        public ActionResult Headshots()
        {
            var rawUsers = db.T_Users;

            var userEditModelList = rawUsers
                .Project().To<Models.UsersEditModel>();

            return View(userEditModelList);
        }

        // GET: Users/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            T_Users t_Users =  db.T_Users.Find(id);
            if (t_Users == null)
            {
                return HttpNotFound();
            }
            return View(t_Users);
        }

        // GET: Users/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Users/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "user_ID,user_Name,user_Admin,user_Email,user_Birthday")] T_Users t_Users)
        {
            if (ModelState.IsValid)
            {
                db.T_Users.Add(t_Users);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(t_Users);
        }

        // GET: Users/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            //T_Users t_Users = db.T_Users.Find(id);

            var userEditModelList = db.T_Users
                .Where(a => a.user_ID == id)
                .Project().To<Models.UsersEditModel>();
            var userEditModel = userEditModelList
                .FirstOrDefault();

            return View(userEditModel);
        }

        // POST: Users/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "user_ID,user_Name,user_Admin,user_Email,user_Birthday")] T_Users t_Users)
        {
            if (ModelState.IsValid)
            {
                db.Entry(t_Users).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(t_Users);
        }

        // GET: Users/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            T_Users t_Users = db.T_Users.Find(id);
            if (t_Users == null)
            {
                return HttpNotFound();
            }
            return View(t_Users);
        }

        // POST: Users/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            T_Users t_Users = db.T_Users.Find(id);
            db.T_Users.Remove(t_Users);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
